import router from '../router/index'
const permission = {
  state: {
    all_menu_list: [], // 全局所有菜单权限
    all_buttons_list: [] // 全局所有按钮权限
  },
  getters:{
  all_menu_list: state => state.permission.all_menu_list,
  },
  actions: {},
  mutations: {
    set_global_menu(state, data) {
      state.all_menu_list = data
      let effectiveRoutes = regroupRoute(data)
      let base = [
       
      ]
      base[0].children = [...effectiveRoutes]
      router.addRoute(...base) // 动态添加路由
    },
    set_global_buttons(state, data) {
      state.all_buttons_list = data
    }
  }
}
export default permission

const regroupRoute = (effective) => {
  let realRoutes = []
  effective.forEach((item) => {
    // if (!item.path) {
    //   console.log(item.name)
    // }
    let source = item.source.split('/pages')[1]
    let leaf = {
      path: item.path,
      name: item.name,
      component: () => import(`@/pages${source}`)
    }
    if (item.children && item.children.length > 0) {
      leaf['children'] = regroupRoute(item.children)
    }
    realRoutes.push(leaf)
  })
  return realRoutes
}
